package com.dmall.controller;

import com.dmall.domain.User;
import com.dmall.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用于管理Controller
 *
 * @author xiao1.wang@dmall.com
 * @date 2019-03-21 18:29:37
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    @PostMapping("/save")
    public String save(@RequestBody User user) {
        log.info("user = " + user);
        userService.save(user);
        return "save success!";
    }

    @GetMapping("/init")
    public String initUser() {
        User u1 = new User();
        u1.setLoginName("zhangsan");
        u1.setPassword("zhangsan");
        u1.setRealName("张三");
        u1.setPhone("16666666666");
        u1.setEmail("zhangsan@163.com");
        u1.setBirth(new Date());

        User u2 = new User();
        u2.setLoginName("lisi");
        u2.setPassword("lisi");
        u2.setRealName("李四");
        u2.setPhone("17777777777");
        u2.setEmail("zhangsan@tencent.com");
        u2.setBirth(new Date());

        User u3 = new User();
        u3.setLoginName("wangwu");
        u3.setPassword("wangwu");
        u3.setRealName("王五");
        u3.setPhone("18888888888");
        u3.setEmail("wangwu@dmall.com");
        u3.setBirth(new Date());

        List<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);
        list.add(u3);

        userService.save(list);
        return "save multi user success!";
    }
}
